package com.x.erp.dao;

import com.x.erp.model.InsuranceGoodsVoucher;
import com.x.erp.model.InsuranceOrderGoods;
import com.x.erp.util.ConstantState;
import com.x.framework.dao.BaseDao;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 延保商品使用券dao
 *
 * @author pansong
 */
@Repository
public class InsuranceGoodsVoucherDao extends BaseDao {

	/**
	 * 插入多条订单商品使用券信息
	 */
	private final static String insertVoucherListSql = "INSERT INTO ERP_INSURANCE_GOODS_VOUCHER (VOUCHER_ID,VOUCHER_NAME,ORDER_ID,ORDER_GOODS_ID,VOUCHER_VERIFICATION_STATE,VOUCHER_CREATE_TIME,VOUCHER_UPDATE_TIME) VALUES ";

	public int insertVoucherList(List<InsuranceOrderGoods> orderGoodsList) throws Exception {
		StringBuilder sql = new StringBuilder(insertVoucherListSql);
		List<Object> params = new ArrayList<>();
		if (orderGoodsList == null || orderGoodsList.size() == 0) {
			return 0;
		}
		appendInsertSql(sql, params, orderGoodsList);
		return this.execute(sql.toString(), params);
	}

	// 拼接插入语句
	private void appendInsertSql(StringBuilder sql, List<Object> params, List<InsuranceOrderGoods> orderGoodsList) {
		for (InsuranceOrderGoods orderGoodsTemp : orderGoodsList) {
			for (int i = 0; i < orderGoodsTemp.getOrderGoodsNum(); i++) {
				sql.append("(?,?,?,?,?,?,?),");
				params.add(this.getUid(32));
				// 生成使用券名称
				String goodsName = orderGoodsTemp.getGoodsName();
				String str = String.format("%03d", i + 1);
				params.add(goodsName + str);
				params.add(orderGoodsTemp.getOrderId());
				params.add(orderGoodsTemp.getOderGoodsId());
				// 未核销
				params.add(ConstantState.INSURANCE_VERIFICATION_STATE_INITIAL);
				Date cDate = new Date();
				params.add(cDate);
				params.add(cDate);
			}
		}
		sql.deleteCharAt(sql.length() - 1);
	}

	/**
	 * 查询延保商品使用券列表
	 *
	 * @param insuranceGoodsVoucher
	 * @return
	 */
	private final static String selectVoucherListSql = "SELECT eigv.* FROM ERP_INSURANCE_GOODS_VOUCHER eigv "
			+ "WHERE 1 = 1 ";

	public List<InsuranceGoodsVoucher> selectVoucherList(InsuranceGoodsVoucher insuranceGoodsVoucher) throws Exception {
		StringBuilder sql = new StringBuilder(selectVoucherListSql);
		List<Object> params = new ArrayList<>();
		selectSearch(sql, params, insuranceGoodsVoucher);
		return this.queryForList(sql.toString(), InsuranceGoodsVoucher.class, params, insuranceGoodsVoucher);
	}

	/**
	 * 查询延保商品使用券总数
	 *
	 * @param insuranceGoodsVoucher
	 * @return
	 */
	private final static String selectVoucherNumSql = "SELECT COUNT(1) FROM ERP_INSURANCE_GOODS_VOUCHER eigv "
			+ "WHERE 1 = 1 ";

	public int selectVoucherNum(InsuranceGoodsVoucher insuranceGoodsVoucher) throws Exception {
		StringBuilder sql = new StringBuilder(selectVoucherNumSql);
		List<Object> params = new ArrayList<>();
		selectSearch(sql, params, insuranceGoodsVoucher);
		return this.queryForInt(sql.toString(), params);
	}

	/**
	 * 拼接查询条件
	 *
	 * @param sql
	 * @param params
	 * @param insuranceGoodsVoucher
	 */
	private void selectSearch(StringBuilder sql, List<Object> params, InsuranceGoodsVoucher insuranceGoodsVoucher) {
		if (this.isNotNull(insuranceGoodsVoucher.getVoucherId())) {
			sql.append("AND eigv.VOUCHER_ID = ? ");
			params.add(insuranceGoodsVoucher.getVoucherId());
		}
		if (this.isNotNull(insuranceGoodsVoucher.getVoucherName())) {
			sql.append("AND eigv.VOUCHER_NAME = ? ");
			params.add(insuranceGoodsVoucher.getVoucherName());
		}
		if (this.isNotNull(insuranceGoodsVoucher.getOrderId())) {
			sql.append("AND eigv.ORDER_ID = ? ");
			params.add(insuranceGoodsVoucher.getOrderId());
		}
		if (this.isNotNull(insuranceGoodsVoucher.getOrderGoodsId())) {
			sql.append("AND eigv.ORDER_GOODS_ID = ? ");
			params.add(insuranceGoodsVoucher.getOrderGoodsId());
		}
		if (this.isNotNull(insuranceGoodsVoucher.getVoucherVerificationState())) {
			sql.append("AND eigv.VOUCHER_VERIFICATION_STATE = ? ");
			params.add(insuranceGoodsVoucher.getVoucherVerificationState());
		}
	}
}
